<?php

namespace common\model;

use think\model\RelationModel;

class ChatModel extends RelationModel
{
    //自动验证
    protected $_validate = [
        ['global_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['link_id', '', '', self::EXISTS_VALIDATE, 'integer'],
        ['link_type', '1,255', '', self::EXISTS_VALIDATE, 'length'],
        ['global_id', '1,36', '', self::EXISTS_VALIDATE, 'length'],
        ['global_id', '', '', self::EXISTS_VALIDATE, 'unique'],
        ['name', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['name', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['type', ['system', 'one2one', 'group', 'project'], '', self::EXISTS_VALIDATE, 'in'],
        ['public', [0, 1], '', self::EXISTS_VALIDATE, 'in'],
        ['json', '', '', self::EXISTS_VALIDATE, 'array']
    ];

    // 自动完成
    protected $_auto = [
        ['edited_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['json', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['created', 'time', self::MODEL_INSERT, 'function']
    ];

    /**
     * 获取器：编辑时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getEditedAtAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：创建时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getCreatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：消息内容
     * @param $value
     * @return array|mixed
     */
    public function getJsonAttr($value)
    {
        if (!empty($value)) {
            return json_decode($value, true);
        }
        return [];
    }
}
